import ibissysprops
from jarray import *
from ibis.mpj import *

intervals = zeros(1, 'i')
pi = zeros(1, 'd')

MPJ.init([])
size = MPJ.COMM_WORLD.size()
rank = MPJ.COMM_WORLD.rank()
intervals = zeros(1, 'i')

if rank == 0:
    intervals[0] = 10000

MPJ.COMM_WORLD.bcast(intervals, 0, 1, MPJ.INT, 0)

n = intervals[0]
h = 1.0 / n
sum = 0.0
for i in range(rank, n, size):
    x = h * (i + 0.5)
    sum += 4.0 /(1.0 + x * x)
mypi = array([h * sum], 'd')

MPJ.COMM_WORLD.reduce(mypi, 0, pi, 0,  1, MPJ.DOUBLE, MPJ.SUM, 0)

if rank == 0:
    print pi[0]
    
MPJ.finish()
